package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlCjljgxrrkljlbBaDetail;
import com.ruoyi.report_jl.domain.vo.ReportJlCjljgxrrkljlbBaDetailVo;
import com.ruoyi.report_jl.mapper.ReportJlCjljgxrrkljlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjljgxrrkljlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjljgxrrkljlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlCjljgxrrkljlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlCjljgxrrkljlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlCjljgxrrkljlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料坚固性、软弱颗粒含量试验检测记录Service业务层处理
 *
 * @author tll
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjljgxrrkljlbBsInfoServiceImpl implements IReportJlCjljgxrrkljlbBsInfoService {

    private final ReportJlCjljgxrrkljlbBsInfoMapper baseMapper;
    private final ReportJlCjljgxrrkljlbBaDetailMapper reportJlCjljgxrrkljlbBaDetailMapper;

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportJlCjljgxrrkljlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportJlCjljgxrrkljlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportJlCjljgxrrkljlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportJlCjljgxrrkljlbBaDetail::getCreateTime);
        List<ReportJlCjljgxrrkljlbBaDetailVo> detailList = reportJlCjljgxrrkljlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportJlCjljgxrrkljlbBsInfoVo> queryPageList(ReportJlCjljgxrrkljlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjljgxrrkljlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录列表
     */
    @Override
    public List<ReportJlCjljgxrrkljlbBsInfoVo> queryList(ReportJlCjljgxrrkljlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjljgxrrkljlbBsInfo> buildQueryWrapper(ReportJlCjljgxrrkljlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlCjljgxrrkljlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlCjljgxrrkljlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlCjljgxrrkljlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlCjljgxrrkljlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlCjljgxrrkljlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlCjljgxrrkljlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlCjljgxrrkljlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlCjljgxrrkljlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlCjljgxrrkljlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlCjljgxrrkljlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlCjljgxrrkljlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlCjljgxrrkljlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlCjljgxrrkljlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlCjljgxrrkljlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlCjljgxrrkljlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlCjljgxrrkljlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlCjljgxrrkljlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlCjljgxrrkljlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlCjljgxrrkljlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlCjljgxrrkljlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlCjljgxrrkljlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlCjljgxrrkljlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlCjljgxrrkljlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlCjljgxrrkljlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getShiyzzlItem1() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyzzlItem1, bo.getShiyzzlItem1());
        lqw.eq(bo.getGongcljItem1() != null, ReportJlCjljgxrrkljlbBsInfo::getGongcljItem1, bo.getGongcljItem1());
        lqw.eq(bo.getShiygljklzzlItem1() != null, ReportJlCjljgxrrkljlbBsInfo::getShiygljklzzlItem1, bo.getShiygljklzzlItem1());
        lqw.eq(bo.getShiyhgljklzzlItem1() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyhgljklzzlItem1, bo.getShiyhgljklzzlItem1());
        lqw.eq(bo.getRuanrklhlczItem1() != null, ReportJlCjljgxrrkljlbBsInfo::getRuanrklhlczItem1, bo.getRuanrklhlczItem1());
        lqw.eq(bo.getShiyzzlItem2() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyzzlItem2, bo.getShiyzzlItem2());
        lqw.eq(bo.getGongcljItem2() != null, ReportJlCjljgxrrkljlbBsInfo::getGongcljItem2, bo.getGongcljItem2());
        lqw.eq(bo.getShiygljklzzlItem2() != null, ReportJlCjljgxrrkljlbBsInfo::getShiygljklzzlItem2, bo.getShiygljklzzlItem2());
        lqw.eq(bo.getShiyhgljklzzlItem2() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyhgljklzzlItem2, bo.getShiyhgljklzzlItem2());
        lqw.eq(bo.getRuanrklhlczItem2() != null, ReportJlCjljgxrrkljlbBsInfo::getRuanrklhlczItem2, bo.getRuanrklhlczItem2());
        lqw.eq(bo.getShiyzzlItem3() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyzzlItem3, bo.getShiyzzlItem3());
        lqw.eq(bo.getGongcljItem3() != null, ReportJlCjljgxrrkljlbBsInfo::getGongcljItem3, bo.getGongcljItem3());
        lqw.eq(bo.getShiygljklzzlItem3() != null, ReportJlCjljgxrrkljlbBsInfo::getShiygljklzzlItem3, bo.getShiygljklzzlItem3());
        lqw.eq(bo.getShiyhgljklzzlItem3() != null, ReportJlCjljgxrrkljlbBsInfo::getShiyhgljklzzlItem3, bo.getShiyhgljklzzlItem3());
        lqw.eq(bo.getRuanrklhlczItem3() != null, ReportJlCjljgxrrkljlbBsInfo::getRuanrklhlczItem3, bo.getRuanrklhlczItem3());
        return lqw;
    }

    /**
     * 新增粗集料坚固性、软弱颗粒含量试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportJlCjljgxrrkljlbBsInfoBo bo) {
        ReportJlCjljgxrrkljlbBsInfo add = BeanUtil.toBean(bo, ReportJlCjljgxrrkljlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改粗集料坚固性、软弱颗粒含量试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportJlCjljgxrrkljlbBsInfoBo bo) {
        // 主表
        ReportJlCjljgxrrkljlbBsInfo update = BeanUtil.toBean(bo, ReportJlCjljgxrrkljlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean upd = baseMapper.updateById(update) > 0;
        // 子表
        boolean upd2 = false;
        boolean insert = false;
        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
            // 根据excelId查询明细
            QueryWrapper<ReportJlCjljgxrrkljlbBaDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("excel_id", bo.getExcelId());
            List<ReportJlCjljgxrrkljlbBaDetail> list = reportJlCjljgxrrkljlbBaDetailMapper.selectList(queryWrapper);
            for (ReportJlCjljgxrrkljlbBaDetail det : bo.getDetailList()) {
                if (ObjectUtils.isEmpty(list)) {
                    // 新增
                    insert = reportJlCjljgxrrkljlbBaDetailMapper.insert(det) > 0;
                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
                    // 修改
                    upd2 = reportJlCjljgxrrkljlbBaDetailMapper.updateById(det) > 0;
                } else {
                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
                }
            }
        }
        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjljgxrrkljlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料坚固性、软弱颗粒含量试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
